use std::env;
use std::process::Command;
use std::process::Output;

pub fn command_demo() {
  test_copy_dir();
}

fn test1() {
  let output = Command::new("sh")
    .arg("pwd")
    .output()
    .expect("exec is failed.");
  println!("{:?}", output);
}
fn test2() {
  let output = Command::new("sh")
    .arg("-c")
    .arg("pwd")
    .output()
    .expect("exec is failed.");
  println!("{:?}", output);
}

fn test_insnt() {
  let output = Command::new("sh")
    .arg("-c")
    .arg("install_name_tool -add_rpath /Users/zuoyu/tmp/rust/test4/xxxxbindgen_demo/target/debug4 /Users/zuoyu/tmp/rust/test4/xxxxbindgen_demo/target/debug/xxxxbindgen_demo")
    .output()
    .expect("install_name_tool is failed.");
  println!("{:?}", output);
}

fn test_insnt_2() {
  let executable_path = env::current_exe().expect("executable_path");
  println!("{:?}", executable_path.as_os_str().to_str());
  let rpath = "@executable_path";
  let executable = "/Users/zuoyu/tmp/rust/test4/xxxxbindgen_demo/target/debug/xxxxbindgen_demo";
  let mut params = String::from("install_name_tool -add_rpath ");
  params += rpath;
  params += " ";
  params += executable;
  let output = Command::new("sh")
    .arg("-c")
    .arg(params)
    .output()
    .expect("install_name_tool is failed.");
  println!("{:?}", output);
}

fn test_insnt_3() {
  let executable_path_3 =
    "/Users/zuoyu/tmp/rust/test4/xxxxbindgen_demo/target/debug/xxxxbindgen_demo";
  let rpath = "@executable_path";
  let executable = executable_path_3;
  let mut params = String::from("install_name_tool -add_rpath ");
  params += rpath;
  params += " ";
  params += executable;
  let output = Command::new("sh").arg("-c").arg(params).output();

  match output {
    Ok(_) => {
      println!("success")
    }
    Err(error) => panic!("{}", error),
  }
}

fn test_copy_dir() {
  let mut params = String::from("cp -Rf ");
  params += "/Users/zuoyu/tmp/rust/http-server/routinglib";
  params += " ";
  params += "/Users/zuoyu/tmp/rust/http-server/target/debug";
  Command::new("sh")
    .arg("-c")
    .arg(params)
    .output()
    .expect("copy files is failed.");
}
